package visaoInterface;

import dao.DaoPessoa;
import dao.DaoEndereco;
import dao.DaoLocacao;
import dao.DaoMotorista;
import modelo.dominio.Pessoa;
import modelo.dominio.Motorista;
import javax.swing.JOptionPane;
import modelo.dominio.Cliente;
import modelo.dominio.Endereco;


public class ChamaMotorista extends javax.swing.JDialog {

    /** Creates new form ChamaMotorista */
    public ChamaMotorista(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /**
     * Metodo que salva os dados do motorista no banco de dados a partir
     * do que o usuario digita nas caixas de texto.
     */
    public void salvarMotorista(){
        if(jTFMNome.getText().equals("") || jTFMCpf.getText().equals("") || jTFCnh.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencher os campos obrigatórios.");
        }else{
                
        String cpf = jTFMCpf.getText();        
        if (cpf.length() > 11){
        cpf = cpf.replaceAll("\\.", "");
        cpf = cpf.replaceAll("-","");
        }
        Pessoa p = new Pessoa();
        if (p.verificaCpf(cpf) && p.validaCpf(cpf)){ 
            int cod;
            Motorista m = new Motorista();
            Endereco e = new Endereco();
            DaoMotorista dm = new DaoMotorista();
            DaoPessoa dp = new DaoPessoa();
            DaoEndereco de = new DaoEndereco();
            cod = de.retornaCodUltimoRegistro();
            p.setNome(jTFMNome.getText());
            p.setCpf(jTFMCpf.getText());
            p.setTelefone(jTFMTelefone.getText());
            m.setCpf(jTFMCpf.getText());
            m.setCnh(jTFCnh.getText());
            e.setRua(jTFMRua.getText());
            if (jTFMNumero.getText().equals("")){
                e.setNumero(0);
            }else{
                e.setNumero(Integer.parseInt(jTFMNumero.getText()));
            }
            e.setBairro(jTFMBairro.getText());
            de.insere(e);
            dm.insere(m);
            cod = de.retornaCodUltimoRegistro();
            int codMotorista = dm.retornaCodUltimoRegistro();
            dp.insereMotorista(p, cod, codMotorista);

            jTFMNome.setText("");
            jTFMCpf.setText("");
            jTFMTelefone.setText("");
            jTFMRua.setText("");
            jTFMNumero.setText("");
            jTFMBairro.setText("");
            jTFCnh.setText("");
            JOptionPane.showMessageDialog(null, "Motorista cadastrado com sucesso.");
        }else{
        JOptionPane.showMessageDialog(null, "CPF não corresponde a um CPF valido.");
        jTFMCpf.setText("");
        }
        }        
    }
    
    
    
   
    /**
     * Metodo que limpa as caixas de texto da interface.
     */
    public void limpaDadosCaixaTexto(){
        jTFMNome.setText("");
        jTFMCpf.setText("");
        jTFMTelefone.setText("");
        jTFMRua.setText("");
        jTFMNumero.setText("");
        jTFMBairro.setText("");
        jTFCnh.setText("");
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLMNome = new javax.swing.JLabel();
        jLMCpf = new javax.swing.JLabel();
        jLMTelefone = new javax.swing.JLabel();
        jLMRua = new javax.swing.JLabel();
        jLMNumero = new javax.swing.JLabel();
        jTFMNome = new javax.swing.JTextField();
        jTFMRua = new javax.swing.JTextField();
        jLMBairro = new javax.swing.JLabel();
        jTFMBairro = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLCnh = new javax.swing.JLabel();
        jBMCancelar = new javax.swing.JButton();
        jBMSalvar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jBPesquisar = new javax.swing.JButton();
        jTFMTelefone = new javax.swing.JFormattedTextField();
        jTFCnh = new javax.swing.JFormattedTextField();
        jTFMCpf = new javax.swing.JFormattedTextField();
        jTFMNumero = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Motorista"));

        jLMNome.setText("Nome*");

        jLMCpf.setText("CPF*");

        jLMTelefone.setText("Telefone");

        jLMRua.setText("Rua");

        jLMNumero.setText("Número");

        jTFMNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFMNomeActionPerformed(evt);
            }
        });

        jLMBairro.setText("Bairro");

        jLabel8.setText("Endereço:");

        jLCnh.setText("CNH*");

        jBMCancelar.setText("Cancelar");
        jBMCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBMCancelarActionPerformed(evt);
            }
        });

        jBMSalvar.setText("Salvar");
        jBMSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBMSalvarActionPerformed(evt);
            }
        });

        jLabel1.setText("* Campos Obrigatórios");

        jBPesquisar.setText("Limpar");
        jBPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisarActionPerformed(evt);
            }
        });

        try {
            jTFMTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jTFCnh.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jTFMCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLMRua)
                                .addGap(27, 27, 27))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLMBairro)
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFMRua, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLMNumero)
                                .addGap(18, 18, 18)
                                .addComponent(jTFMNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFMBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLCnh)
                                .addGap(18, 18, 18)
                                .addComponent(jTFCnh, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addComponent(jBPesquisar)
                        .addGap(18, 18, 18)
                        .addComponent(jBMSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(jBMCancelar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLMNome)
                            .addComponent(jLMCpf))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFMCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLMTelefone)
                                .addGap(18, 18, 18)
                                .addComponent(jTFMTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                            .addComponent(jTFMNome, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFMNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLMNome))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLMCpf)
                    .addComponent(jTFMTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFMCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLMTelefone))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLMRua)
                    .addComponent(jTFMRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLMNumero)
                    .addComponent(jTFMNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFMBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLMBairro)
                    .addComponent(jLCnh)
                    .addComponent(jTFCnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBMCancelar)
                        .addComponent(jBMSalvar)
                        .addComponent(jBPesquisar))
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTFMNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFMNomeActionPerformed

    }//GEN-LAST:event_jTFMNomeActionPerformed

    private void jBMCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBMCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBMCancelarActionPerformed

    private void jBPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisarActionPerformed
        limpaDadosCaixaTexto();
    }//GEN-LAST:event_jBPesquisarActionPerformed

    private void jBMSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBMSalvarActionPerformed
        salvarMotorista();
        this.dispose();
    }//GEN-LAST:event_jBMSalvarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChamaMotorista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChamaMotorista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChamaMotorista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChamaMotorista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChamaMotorista dialog = new ChamaMotorista(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBMCancelar;
    private javax.swing.JButton jBMSalvar;
    private javax.swing.JButton jBPesquisar;
    private javax.swing.JLabel jLCnh;
    private javax.swing.JLabel jLMBairro;
    private javax.swing.JLabel jLMCpf;
    private javax.swing.JLabel jLMNome;
    private javax.swing.JLabel jLMNumero;
    private javax.swing.JLabel jLMRua;
    private javax.swing.JLabel jLMTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JFormattedTextField jTFCnh;
    private javax.swing.JTextField jTFMBairro;
    private javax.swing.JFormattedTextField jTFMCpf;
    private javax.swing.JTextField jTFMNome;
    private javax.swing.JTextField jTFMNumero;
    private javax.swing.JTextField jTFMRua;
    private javax.swing.JFormattedTextField jTFMTelefone;
    // End of variables declaration//GEN-END:variables
}
